﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x020001D1 RID: 465
public class CutSceneEdit_TimeBar : MonoBehaviour
{
	// Token: 0x17000168 RID: 360
	// (get) Token: 0x06000C12 RID: 3090 RVA: 0x000462FC File Offset: 0x000444FC
	// (set) Token: 0x06000C13 RID: 3091 RVA: 0x00046304 File Offset: 0x00044504
	public bool IsReputKeys { get; private set; }

	// Token: 0x17000169 RID: 361
	// (get) Token: 0x06000C14 RID: 3092 RVA: 0x00046310 File Offset: 0x00044510
	// (set) Token: 0x06000C15 RID: 3093 RVA: 0x00046318 File Offset: 0x00044518
	public int ActiveKey { get; private set; }

	// Token: 0x06000C16 RID: 3094 RVA: 0x00046324 File Offset: 0x00044524
	private void Start()
	{
		this.ActiveKey = -1;
		this.endTimeInput.text = this.endTime.ToString();
		this.PutKeys();
		this.UpdateZoomText();
	}

	// Token: 0x06000C17 RID: 3095 RVA: 0x00046358 File Offset: 0x00044558
	private void Update()
	{
		if (this.IsReputKeys)
		{
			this.PutKeys();
		}
		if (this.isPlay && this.nowTime < this.endTime)
		{
			this.nowTime += Time.deltaTime;
			if (this.nowTime >= this.endTime)
			{
				this.nowTime = this.endTime;
				this.isPlay = false;
			}
		}
		this.invoke = false;
		if (this.endTime > 0f)
		{
			this.timeBar.value = Mathf.InverseLerp(0f, this.endTime, this.nowTime);
		}
		else
		{
			this.timeBar.value = 0f;
		}
		if (!this.nowTimeInput.isFocused)
		{
			this.nowTimeInput.text = this.nowTime.ToString("000.00");
		}
		this.UpdateNowTick();
		int num = -1;
		for (int i = 0; i < this.keys.Count; i++)
		{
			if (this.keys[i].toggle.isOn)
			{
				num = i;
				break;
			}
		}
		if (num != this.ActiveKey)
		{
			this.ActiveKey = num;
			if (this.ActiveKey == -1)
			{
				this.edit.command.ClearList();
			}
			else
			{
				float time = this.keys[this.ActiveKey].time;
				this.nowTime = time;
				this.CheckNowTime();
			}
		}
		this.invoke = true;
		if (this.draggingKey != null)
		{
			this.KeyDrag();
		}
	}

	// Token: 0x06000C18 RID: 3096 RVA: 0x000464FC File Offset: 0x000446FC
	public void SetPlay(bool flag)
	{
		this.isPlay = flag;
		if (this.isPlay)
		{
			this.edit.cutScene.IsPlay = true;
		}
	}

	// Token: 0x06000C19 RID: 3097 RVA: 0x00046524 File Offset: 0x00044724
	public void ZoomIn()
	{
		this.zoom += 1f;
		this.zoom = Mathf.Clamp(this.zoom, 1f, 100f);
		this.UpdateZoomText();
		this.IsReputKeys = true;
	}

	// Token: 0x06000C1A RID: 3098 RVA: 0x00046560 File Offset: 0x00044760
	public void ZoomOut()
	{
		this.zoom -= 1f;
		this.zoom = Mathf.Clamp(this.zoom, 1f, 100f);
		this.UpdateZoomText();
		this.IsReputKeys = true;
	}

	// Token: 0x06000C1B RID: 3099 RVA: 0x0004659C File Offset: 0x0004479C
	public void ZoomScroll(BaseEventData data)
	{
		PointerEventData pointerEventData = data as PointerEventData;
		this.zoom += pointerEventData.scrollDelta.y;
		this.zoom = Mathf.Clamp(this.zoom, 1f, 100f);
		this.UpdateZoomText();
		this.IsReputKeys = true;
	}

	// Token: 0x06000C1C RID: 3100 RVA: 0x000465F4 File Offset: 0x000447F4
	private void UpdateZoomText()
	{
		this.zoomText.text = "x" + this.zoom.ToString("0.0");
	}

	// Token: 0x06000C1D RID: 3101 RVA: 0x0004661C File Offset: 0x0004481C
	public void SetEndTime(string str)
	{
		this.endTime = float.Parse(str);
		this.IsReputKeys = true;
	}

	// Token: 0x06000C1E RID: 3102 RVA: 0x00046634 File Offset: 0x00044834
	public void SetNowTime(string str)
	{
		this.nowTime = float.Parse(str);
		this.CheckNowTime();
	}

	// Token: 0x06000C1F RID: 3103 RVA: 0x00046648 File Offset: 0x00044848
	public void SetNowTime(float time)
	{
		this.nowTime = time;
		this.CheckNowTime();
	}

	// Token: 0x06000C20 RID: 3104 RVA: 0x00046658 File Offset: 0x00044858
	public void SetNowTimeRate(float rate)
	{
		this.nowTime = this.endTime * rate;
		this.CheckNowTime();
	}

	// Token: 0x06000C21 RID: 3105 RVA: 0x00046670 File Offset: 0x00044870
	private void CheckNowTime()
	{
		this.edit.command.SetList(this.nowTime);
		this.edit.cutScene.Check(this.nowTime);
		int num = -1;
		for (int i = 0; i < this.keys.Count; i++)
		{
			if (this.keys[i].time == this.nowTime)
			{
				num = i;
			}
		}
		if (num != this.ActiveKey)
		{
			this.invoke = false;
			if (num == -1)
			{
				this.keys[this.ActiveKey].toggle.isOn = false;
			}
			else
			{
				this.keys[num].toggle.isOn = true;
			}
			this.ActiveKey = num;
			this.invoke = true;
		}
	}

	// Token: 0x06000C22 RID: 3106 RVA: 0x00046748 File Offset: 0x00044948
	public void ReputKeys(bool forced)
	{
		this.ActiveKey = -1;
		this.IsReputKeys = true;
		if (forced)
		{
			this.PutKeys();
		}
	}

	// Token: 0x06000C23 RID: 3107 RVA: 0x00046764 File Offset: 0x00044964
	private void TimeEpsilon(ref float time)
	{
		int num = (int)time;
		int num2 = (int)(time % 1f * 100f);
		time = (float)num + (float)num2 * 0.01f;
	}

	// Token: 0x06000C24 RID: 3108 RVA: 0x00046794 File Offset: 0x00044994
	private void PutKeys()
	{
		for (int i = 0; i < this.keys.Count; i++)
		{
			UnityEngine.Object.Destroy(this.keys[i].toggle.gameObject);
		}
		this.keys.Clear();
		for (int j = 0; j < this.ticks.Count; j++)
		{
			UnityEngine.Object.Destroy(this.ticks[j]);
		}
		this.ticks.Clear();
		float num = 10f;
		float num2 = this.keysMaxW * this.zoom;
		Vector2 sizeDelta = this.keysArea.sizeDelta;
		sizeDelta.x = num2 + num * 2f;
		this.keysArea.sizeDelta = sizeDelta;
		float num3 = -1f;
		float b = this.endTime;
		for (int k = 0; k < this.edit.cutScene.Actions.Count; k++)
		{
			float time = this.edit.cutScene.Actions[k].time;
			int count = this.keys.Count;
			if (num3 != time)
			{
				num3 = time;
				float num4 = Mathf.InverseLerp(0f, b, time);
				Vector3 zero = Vector3.zero;
				zero.x = num + num4 * num2;
				zero.y = 0f;
				CutSceneEdit_Key cutSceneEdit_Key = UnityEngine.Object.Instantiate<CutSceneEdit_Key>(this.keyOriginal);
				cutSceneEdit_Key.Setup(this, count, time);
				cutSceneEdit_Key.AddAction(this.edit.cutScene.Actions[k]);
				cutSceneEdit_Key.gameObject.name = "Key_" + count;
				cutSceneEdit_Key.gameObject.SetActive(true);
				RectTransform rectTransform = cutSceneEdit_Key.transform as RectTransform;
				rectTransform.SetParent(this.keysParent, false);
				rectTransform.anchoredPosition = zero;
				this.keys.Add(cutSceneEdit_Key);
			}
			else
			{
				this.keys[count - 1].AddAction(this.edit.cutScene.Actions[k]);
			}
		}
		if (this.tickNum > 0f)
		{
			float num5 = this.CalcTickTime();
			for (num3 = 0f; num3 <= this.endTime; num3 += num5)
			{
				float num6 = Mathf.InverseLerp(0f, b, num3);
				Vector3 zero2 = Vector3.zero;
				zero2.x = num + num6 * num2;
				zero2.y = 0f;
				RectTransform rectTransform2 = UnityEngine.Object.Instantiate<RectTransform>(this.tickOriginal);
				rectTransform2.gameObject.SetActive(true);
				rectTransform2.SetParent(this.ticksParent, false);
				rectTransform2.anchoredPosition = zero2;
				rectTransform2.GetComponentInChildren<Text>().text = num3.ToString("0.0");
				this.ticks.Add(rectTransform2.gameObject);
			}
		}
		if (this.ActiveKey != -1)
		{
			this.keys[this.ActiveKey].toggle.isOn = true;
		}
		this.IsReputKeys = false;
	}

	// Token: 0x06000C25 RID: 3109 RVA: 0x00046ABC File Offset: 0x00044CBC
	private void UpdateNowTick()
	{
		float num = 10f;
		float num2 = this.keysMaxW * this.zoom;
		float b = this.endTime;
		float num3 = Mathf.InverseLerp(0f, b, this.nowTime);
		Vector3 zero = Vector3.zero;
		zero.x = num + num3 * num2;
		zero.y = 0f;
		this.nowTick.anchoredPosition = zero;
		this.nowTickText.text = this.nowTime.ToString("000.00");
	}

	// Token: 0x06000C26 RID: 3110 RVA: 0x00046B44 File Offset: 0x00044D44
	private float CalcTickTime()
	{
		float num = this.endTime / this.zoom;
		float num2 = num / this.tickNum;
		if (num2 >= 1f)
		{
			int num3 = 0;
			float num4 = num;
			while (num4 > 10f)
			{
				num4 /= 10f;
				num3++;
			}
			num2 /= Mathf.Pow(10f, (float)num3);
			if (num2 < 0.25f)
			{
				num2 = 0.25f;
			}
			else if (num2 < 0.75f)
			{
				num2 = 0.5f;
			}
			else
			{
				num2 = 1f;
			}
			num2 *= Mathf.Pow(10f, (float)num3);
		}
		else if (num2 < 0.1f)
		{
			num2 = 0.1f;
		}
		else if (num2 < 0.25f)
		{
			num2 = 0.5f;
		}
		else if (num2 < 0.75f)
		{
			num2 = 0.5f;
		}
		else
		{
			num2 = 1f;
		}
		return num2;
	}

	// Token: 0x06000C27 RID: 3111 RVA: 0x00046C34 File Offset: 0x00044E34
	public void TimeBarMouseControl(BaseEventData data)
	{
		PointerEventData pointerEventData = data as PointerEventData;
		Vector2 position = pointerEventData.position;
		if (pointerEventData.button == PointerEventData.InputButton.Left)
		{
			float a = this.keysParent.position.x + this.keysParent.rect.xMin + 10f;
			float b = this.keysParent.position.x + this.keysParent.rect.xMax - 10f;
			float num = Mathf.InverseLerp(a, b, position.x);
			num = Mathf.Clamp01(num);
			this.nowTime = this.endTime * num;
			this.CheckNowTime();
		}
		if (pointerEventData.button == PointerEventData.InputButton.Middle)
		{
			float num2 = 1f / this.scrollRect.content.rect.width;
			float num3 = pointerEventData.delta.x * num2;
			this.scrollRect.horizontalScrollbar.value -= num3;
		}
	}

	// Token: 0x06000C28 RID: 3112 RVA: 0x00046D44 File Offset: 0x00044F44
	public void KeyDrag()
	{
		Vector2 vector = Input.mousePosition;
		float a = this.keysParent.position.x + this.keysParent.rect.xMin + 10f;
		float b = this.keysParent.position.x + this.keysParent.rect.xMax - 10f;
		float num = Mathf.InverseLerp(a, b, vector.x);
		num = Mathf.Clamp01(num);
		float time = num * this.endTime;
		this.TimeEpsilon(ref time);
		this.draggingKey.time = time;
		float num2 = 10f;
		float num3 = this.keysMaxW * this.zoom;
		Vector2 anchoredPosition = vector;
		anchoredPosition.x = num2 + num * num3;
		anchoredPosition.y = 0f;
		RectTransform rectTransform = this.draggingKey.transform as RectTransform;
		rectTransform.anchoredPosition = anchoredPosition;
	}

	// Token: 0x06000C29 RID: 3113 RVA: 0x00046E44 File Offset: 0x00045044
	public void BeginKeyDrag(int id)
	{
		this.draggingKey = this.keys[id];
	}

	// Token: 0x06000C2A RID: 3114 RVA: 0x00046E58 File Offset: 0x00045058
	public void EndKeyDrag(int id)
	{
		if (this.draggingKey == this.keys[id])
		{
			float time = this.draggingKey.time;
			this.draggingKey.ChangeActionTime(time);
			this.draggingKey = null;
			this.edit.cutScene.SortActions();
			this.IsReputKeys = true;
		}
	}

	// Token: 0x04000C3C RID: 3132
	[SerializeField]
	private CutSceneEdit edit;

	// Token: 0x04000C3D RID: 3133
	public float endTime = 60f;

	// Token: 0x04000C3E RID: 3134
	public float nowTime;

	// Token: 0x04000C3F RID: 3135
	public bool isPlay;

	// Token: 0x04000C40 RID: 3136
	private bool invoke = true;

	// Token: 0x04000C41 RID: 3137
	[SerializeField]
	private InputField endTimeInput;

	// Token: 0x04000C42 RID: 3138
	[SerializeField]
	private Text zoomText;

	// Token: 0x04000C43 RID: 3139
	[SerializeField]
	private Slider timeBar;

	// Token: 0x04000C44 RID: 3140
	[SerializeField]
	private InputField nowTimeInput;

	// Token: 0x04000C45 RID: 3141
	[SerializeField]
	private RectTransform keysArea;

	// Token: 0x04000C46 RID: 3142
	[SerializeField]
	private ScrollRect scrollRect;

	// Token: 0x04000C47 RID: 3143
	[SerializeField]
	private CutSceneEdit_Key keyOriginal;

	// Token: 0x04000C48 RID: 3144
	[SerializeField]
	private float keysMaxW = 1000f;

	// Token: 0x04000C49 RID: 3145
	[SerializeField]
	private RectTransform keysParent;

	// Token: 0x04000C4A RID: 3146
	[SerializeField]
	private RectTransform tickOriginal;

	// Token: 0x04000C4B RID: 3147
	[SerializeField]
	private float tickNum = 10f;

	// Token: 0x04000C4C RID: 3148
	[SerializeField]
	private RectTransform ticksParent;

	// Token: 0x04000C4D RID: 3149
	[SerializeField]
	private RectTransform nowTick;

	// Token: 0x04000C4E RID: 3150
	[SerializeField]
	private Text nowTickText;

	// Token: 0x04000C4F RID: 3151
	private float zoom = 1f;

	// Token: 0x04000C50 RID: 3152
	private const float zoomMax = 100f;

	// Token: 0x04000C51 RID: 3153
	private List<CutSceneEdit_Key> keys = new List<CutSceneEdit_Key>();

	// Token: 0x04000C52 RID: 3154
	private List<GameObject> ticks = new List<GameObject>();

	// Token: 0x04000C53 RID: 3155
	private CutSceneEdit_Key draggingKey;
}
